import path from "./path.js"
import ajax from "./ajax.js"
import webViewSrc from "./webView.js"

const gotoPage = (url_name,mode=2)=>{
    if (!url_name) {
        return false;
    }
    if ( !path.page.hasOwnProperty(url_name) ){
        return false;
    }
	let url = path.page[url_name];
    switch (mode){
        case 2:
            wx.redirectTo({
                url: url
            })
        break;
        case 3:
            wx.switchTab({
                url: url
            })
        break;
        case 1:
        default:
            wx.navigateTo({
                url: url
            })
        break;
    }
}

const certification = {
    apply : ()=>{
        wx.setStorage({
            key:"certification",
            data:"apply"
        })
    },
    cancel: () => {
        wx.showModal({
            title: '提示',
            content: '你确定要取消实名认证的申请？',
            confirmText:"我已确定",
            cancelText:"容我思考",
            confirmColor: "red",
            cancelColor: "#aaa",
            success: function (res) {
                if (res.confirm) {
                    let status = wx.removeStorageSync("certification")
                    wx.showToast({
                        title: '取消认证成功',
                        icon: 'success',
                        duration: 2000
                    })
                    delay(2000).then(()=>{
                        gotoPage("certification")
                    })
                } else if (res.cancel) {
                    
                }
            }
        })
    },
    ok : ()=>{
        wx.setStorage({
            key:"certification",
            data:"ok"
        })
    },
    status : ()=>{
        return wx.getStorageSync("certification")
    }
}

const delay = function(s){
    return new Promise(function(r,rs){
        setTimeout(r,s)
    });
}


module.exports = {
    ajax,
    gotoPage,
    certification,
    delay,
    webViewSrc
}
